home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.tree;
-
- import com.sun.java.swing.DefaultListSelectionModel;
- import com.sun.java.swing.event.EventListenerList;
- import com.sun.java.swing.event.TreeSelectionEvent;
- import com.sun.java.swing.event.TreeSelectionListener;
- import java.beans.PropertyChangeListener;
- import java.beans.PropertyChangeSupport;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- import java.util.BitSet;
- import java.util.Vector;
-
- public class DefaultTreeSelectionModel implements Cloneable, Serializable, TreeSelectionModel {
- public static final String SELECTION_MODE_PROPERTY = "selectionMode";
- protected PropertyChangeSupport changeSupport;
- protected TreePath[] selection;
- protected EventListenerList listenerList = new EventListenerList();
- protected transient RowMapper rowMapper;
- protected DefaultListSelectionModel listSelectionModel = new DefaultListSelectionModel();
- protected int selectionMode = 4;
- protected TreePath leadPath;
- protected int leadIndex;
- protected int leadRow;
- static Class class$com$sun$java$swing$event$TreeSelectionListener;
-
- public DefaultTreeSelectionModel() {
- this.leadIndex = this.leadRow = -1;
- }
-
- public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
- if (this.changeSupport == null) {
- this.changeSupport = new PropertyChangeSupport(this);
- }
-
- this.changeSupport.addPropertyChangeListener(listener);
- }
-
- public void addSelectionPath(TreePath path) {
- if (path != null) {
- TreePath[] toAdd = new TreePath[1];
- toAdd[0] = path;
- this.addSelectionPaths(toAdd);
- }
-
- }
-
- public void addSelectionPaths(TreePath[] paths) {
- int newPathLength = paths == null ? 0 : paths.length;
- if (newPathLength > 0) {
- if (this.selectionMode == 1 && this.selection != null && this.selection.length > 0) {
- this.setSelectionPaths(paths);
- } else if (this.selectionMode == 2 && !this.canPathsBeAdded(paths)) {
- if (this.arePathsContiguous(paths)) {
- this.setSelectionPaths(paths);
- } else {
- TreePath[] newPaths = new TreePath[1];
- newPaths[0] = paths[0];
- this.setSelectionPaths(newPaths);
- }
- } else {
- TreePath beginLeadPath = this.leadPath;
- Vector cPaths = null;
- int oldCount;
- if (this.selection == null) {
- oldCount = 0;
- } else {
- oldCount = this.selection.length;
- }
-
- boolean newPaths = false;
- this.leadPath = null;
- int counter = 0;
-
- int validCount;
- for(validCount = 0; counter < paths.length; ++counter) {
- if (paths[counter] != null) {
- boolean inSelection = false;
-
- for(int oldCounter = 0; oldCounter < oldCount; ++oldCounter) {
- if (paths[counter].equals(this.selection[oldCounter])) {
- oldCounter = oldCount;
- if (!newPaths) {
- TreePath[] copiedPaths = new TreePath[paths.length];
- System.arraycopy(paths, 0, copiedPaths, 0, paths.length);
- paths = copiedPaths;
- newPaths = true;
- }
-
- paths[counter] = null;
- inSelection = true;
- }
- }
-
- if (!inSelection) {
- ++validCount;
- if (cPaths == null) {
- cPaths = new Vector();
- }
-
- cPaths.addElement(new PathPlaceHolder(paths[counter], true));
- }
-
- if (this.leadPath == null) {
- this.leadPath = paths[counter];
- }
- }
- }
-
- if (this.leadPath == null) {
- this.leadPath = beginLeadPath;
- }
-
- if (validCount > 0) {
- TreePath[] newSelection = new TreePath[oldCount + validCount];
- if (oldCount > 0) {
- System.arraycopy(this.selection, 0, newSelection, 0, oldCount);
- }
-
- if (validCount != paths.length) {
- int validCounter = 0;
-
- for(int var14 = 0; var14 < paths.length; ++var14) {
- if (paths[var14] != null) {
- newSelection[oldCount + validCounter++] = paths[var14];
- }
- }
- } else {
- System.arraycopy(paths, 0, newSelection, oldCount, validCount);
- }
-
- this.selection = newSelection;
- this.insureUniqueness();
- this.updateLeadIndex();
- this.resetRowSelection();
- this.notifyPathChange(cPaths, beginLeadPath);
- } else {
- this.leadPath = beginLeadPath;
- }
- }
- }
-
- }
-
- public void addTreeSelectionListener(TreeSelectionListener x) {
- EventListenerList var10000 = this.listenerList;
- Class var10001 = class$com$sun$java$swing$event$TreeSelectionListener;
- if (var10001 == null) {
- try {
- var10001 = Class.forName("com.sun.java.swing.event.TreeSelectionListener");
- } catch (ClassNotFoundException var2) {
- throw new NoClassDefFoundError(((Throwable)var2).getMessage());
- }
-
- class$com$sun$java$swing$event$TreeSelectionListener = var10001;
- }
-
- var10000.add(var10001, x);
- }
-
- protected boolean arePathsContiguous(TreePath[] paths) {
- if (this.rowMapper != null && paths.length >= 2) {
- BitSet bitSet = new BitSet(32);
- int pathCount = paths.length;
- int validCount = 0;
- TreePath[] tempPath = new TreePath[]{paths[0]};
- int min = this.rowMapper.getRowsForPaths(tempPath)[0];
-
- for(int counter = 0; counter < pathCount; ++counter) {
- if (paths[counter] != null) {
- tempPath[0] = paths[counter];
- int anIndex = this.rowMapper.getRowsForPaths(tempPath)[0];
- if (anIndex == -1 || anIndex < min - pathCount || anIndex > min + pathCount) {
- return false;
- }
-
- if (anIndex < min) {
- min = anIndex;
- }
-
- if (!bitSet.get(anIndex)) {
- bitSet.set(anIndex);
- ++validCount;
- }
- }
- }
-
- int maxCounter = validCount + min;
-
- for(int var10 = min; var10 < maxCounter; ++var10) {
- if (!bitSet.get(var10)) {
- return false;
- }
- }
-
- return true;
- } else {
- return true;
- }
- }
-
- protected boolean canPathsBeAdded(TreePath[] paths) {
- if (paths != null && paths.length != 0 && this.rowMapper != null && this.selection != null && this.selectionMode != 4) {
- BitSet bitSet = new BitSet();
- DefaultListSelectionModel lModel = this.listSelectionModel;
- int min = lModel.getMinSelectionIndex();
- int max = lModel.getMaxSelectionIndex();
- TreePath[] tempPath = new TreePath[1];
- if (min != -1) {
- for(int counter = min; counter <= max; ++counter) {
- if (lModel.isSelectedIndex(min)) {
- bitSet.set(counter);
- }
- }
- } else {
- tempPath[0] = paths[0];
- min = max = this.rowMapper.getRowsForPaths(tempPath)[0];
- }
-
- for(int var9 = paths.length - 1; var9 >= 0; --var9) {
- if (paths[var9] != null) {
- tempPath[0] = paths[var9];
- int anIndex = this.rowMapper.getRowsForPaths(tempPath)[0];
- min = Math.min(anIndex, min);
- max = Math.max(anIndex, max);
- if (anIndex == -1) {
- return false;
- }
-
- bitSet.set(anIndex);
- }
- }
-
- for(int var10 = min; var10 <= max; ++var10) {
- if (!bitSet.get(var10)) {
- return false;
- }
- }
-
- return true;
- } else {
- return true;
- }
- }
-
- protected boolean canPathsBeRemoved(TreePath[] paths) {
- if (this.rowMapper != null && this.selection != null && this.selectionMode != 4) {
- BitSet bitSet = new BitSet();
- int pathCount = paths.length;
- int min = -1;
- int validCount = 0;
- TreePath[] tPaths = new TreePath[pathCount];
- TreePath[] tempPath = new TreePath[1];
- System.arraycopy(paths, 0, tPaths, 0, pathCount);
-
- for(int counter = this.selection.length - 1; counter >= 0; --counter) {
- boolean found = false;
-
- for(int rCounter = 0; rCounter < pathCount; ++rCounter) {
- if (tPaths[rCounter] != null && this.selection[counter].equals(tPaths[rCounter])) {
- tPaths[rCounter] = null;
- found = true;
- break;
- }
- }
-
- if (!found) {
- tempPath[0] = this.selection[counter];
- int anIndex = this.rowMapper.getRowsForPaths(tempPath)[0];
- if (anIndex != -1 && !bitSet.get(anIndex)) {
- ++validCount;
- if (min == -1) {
- min = anIndex;
- } else {
- min = Math.min(min, anIndex);
- }
-
- bitSet.set(anIndex);
- }
- }
- }
-
- if (validCount > 1) {
- for(int var12 = min + validCount - 1; var12 >= min; --var12) {
- if (!bitSet.get(var12)) {
- return false;
- }
- }
- }
-
- return true;
- } else {
- return true;
- }
- }
-
- public void clearSelection() {
- if (this.selection != null) {
- int selSize = this.selection.length;
- boolean[] newness = new boolean[selSize];
-
- for(int counter = 0; counter < selSize; ++counter) {
- newness[counter] = false;
- }
-
- TreeSelectionEvent event = new TreeSelectionEvent(this, this.selection, newness, this.leadPath, (TreePath)null);
- this.leadPath = null;
- this.leadIndex = this.leadRow = -1;
- this.selection = null;
- this.resetRowSelection();
- this.fireValueChanged(event);
- }
-
- }
-
- public Object clone() throws CloneNotSupportedException {
- DefaultTreeSelectionModel clone = (DefaultTreeSelectionModel)super.clone();
- clone.changeSupport = null;
- if (this.selection != null) {
- int selLength = this.selection.length;
- clone.selection = new TreePath[selLength];
- System.arraycopy(this.selection, 0, clone.selection, 0, selLength);
- }
-
- clone.listenerList = new EventListenerList();
- clone.listSelectionModel = (DefaultListSelectionModel)this.listSelectionModel.clone();
- return clone;
- }
-
- protected void fireValueChanged(TreeSelectionEvent e) {
- Object[] listeners = this.listenerList.getListenerList();
-
- for(int i = listeners.length - 2; i >= 0; i -= 2) {
- Object var10000 = listeners[i];
- Class var10001 = class$com$sun$java$swing$event$TreeSelectionListener;
- if (var10001 == null) {
- try {
- var10001 = Class.forName("com.sun.java.swing.event.TreeSelectionListener");
- } catch (ClassNotFoundException var4) {
- throw new NoClassDefFoundError(((Throwable)var4).getMessage());
- }
-
- class$com$sun$java$swing$event$TreeSelectionListener = var10001;
- }
-
- if (var10000 == var10001) {
- ((TreeSelectionListener)listeners[i + 1]).valueChanged(e);
- }
- }
-
- }
-
- public TreePath getLeadSelectionPath() {
- return this.leadPath;
- }
-
- public int getLeadSelectionRow() {
- return this.leadRow;
- }
-
- public int getMaxSelectionRow() {
- return this.listSelectionModel.getMaxSelectionIndex();
- }
-
- public int getMinSelectionRow() {
- return this.listSelectionModel.getMinSelectionIndex();
- }
-
- public RowMapper getRowMapper() {
- return this.rowMapper;
- }
-
- public int getSelectionCount() {
- return this.selection == null ? 0 : this.selection.length;
- }
-
- public int getSelectionMode() {
- return this.selectionMode;
- }
-
- public TreePath getSelectionPath() {
- return this.selection != null ? this.selection[0] : null;
- }
-
- public TreePath[] getSelectionPaths() {
- if (this.selection != null) {
- int pathSize = this.selection.length;
- TreePath[] result = new TreePath[pathSize];
- System.arraycopy(this.selection, 0, result, 0, pathSize);
- return result;
- } else {
- return null;
- }
- }
-
- public int[] getSelectionRows() {
- return this.rowMapper != null && this.selection != null ? this.rowMapper.getRowsForPaths(this.selection) : null;
- }
-
- protected void insureRowContinuity() {
- if (this.selectionMode == 2 && this.selection != null && this.rowMapper != null) {
- DefaultListSelectionModel lModel = this.listSelectionModel;
- int min = lModel.getMinSelectionIndex();
- if (min != -1) {
- int counter = min;
-
- for(int maxCounter = lModel.getMaxSelectionIndex(); counter <= maxCounter; ++counter) {
- if (!lModel.isSelectedIndex(counter)) {
- if (counter != min) {
- TreePath[] newSel = new TreePath[counter - min];
- System.arraycopy(this.selection, 0, newSel, 0, counter - min);
- this.setSelectionPaths(newSel);
- break;
- }
-
- this.clearSelection();
- }
- }
- }
- } else if (this.selectionMode == 1 && this.selection != null && this.selection.length > 1) {
- this.setSelectionPath(this.selection[0]);
- }
-
- }
-
- protected void insureUniqueness() {
- int dupCount = 0;
-
- for(int compareCounter = 0; compareCounter < this.selection.length; ++compareCounter) {
- if (this.selection[compareCounter] != null) {
- for(int indexCounter = compareCounter + 1; indexCounter < this.selection.length; ++indexCounter) {
- if (this.selection[indexCounter] != null && this.selection[compareCounter].equals(this.selection[indexCounter])) {
- ++dupCount;
- this.selection[indexCounter] = null;
- }
- }
- }
- }
-
- if (dupCount > 0) {
- TreePath[] newSelection = new TreePath[this.selection.length - dupCount];
- int counter = 0;
-
- for(int validCounter = 0; counter < this.selection.length; ++counter) {
- if (this.selection[counter] != null) {
- newSelection[validCounter++] = this.selection[counter];
- }
- }
-
- this.selection = newSelection;
- }
-
- }
-
- public boolean isPathSelected(TreePath path) {
- if (this.selection != null) {
- for(int counter = 0; counter < this.selection.length; ++counter) {
- if (this.selection[counter].equals(path)) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- public boolean isRowSelected(int row) {
- return this.listSelectionModel.isSelectedIndex(row);
- }
-
- public boolean isSelectionEmpty() {
- return this.selection == null;
- }
-
- protected void notifyPathChange(Vector changedPaths, TreePath oldLeadSelection) {
- int cPathCount = changedPaths.size();
- boolean[] newness = new boolean[cPathCount];
- TreePath[] paths = new TreePath[cPathCount];
-
- for(int counter = 0; counter < cPathCount; ++counter) {
- PathPlaceHolder placeholder = (PathPlaceHolder)changedPaths.elementAt(counter);
- newness[counter] = placeholder.isNew;
- paths[counter] = placeholder.path;
- }
-
- TreeSelectionEvent event = new TreeSelectionEvent(this, paths, newness, oldLeadSelection, this.leadPath);
- this.fireValueChanged(event);
- }
-
- private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
- s.defaultReadObject();
- Object[] tValues = s.readObject();
- if (tValues.length > 0 && tValues[0].equals("rowMapper")) {
- this.rowMapper = (RowMapper)tValues[1];
- }
-
- }
-
- public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
- if (this.changeSupport != null) {
- this.changeSupport.removePropertyChangeListener(listener);
- }
- }
-
- public void removeSelectionPath(TreePath path) {
- if (path != null) {
- TreePath[] rPath = new TreePath[1];
- rPath[0] = path;
- this.removeSelectionPaths(rPath);
- }
-
- }
-
- public void removeSelectionPaths(TreePath[] paths) {
- if (paths != null && this.selection != null && paths.length > 0) {
- if (!this.canPathsBeRemoved(paths)) {
- this.clearSelection();
- } else {
- TreePath beginLeadPath = this.leadPath;
- Vector pathsToRemove = null;
- int oldCount = this.selection.length;
-
- for(int removeCounter = 0; removeCounter < paths.length; ++removeCounter) {
- if (paths[removeCounter] != null) {
- if (this.leadPath != null && this.leadPath.equals(paths[removeCounter])) {
- this.leadPath = null;
- }
-
- for(int oldCounter = 0; oldCounter < oldCount; ++oldCounter) {
- if (paths[removeCounter].equals(this.selection[oldCounter])) {
- this.selection[oldCounter] = null;
- oldCounter = oldCount;
- if (pathsToRemove == null) {
- pathsToRemove = new Vector(paths.length);
- }
-
- if (!pathsToRemove.contains(paths[removeCounter])) {
- pathsToRemove.addElement(new PathPlaceHolder(paths[removeCounter], false));
- }
- }
- }
- }
- }
-
- if (pathsToRemove != null) {
- int removeCount = pathsToRemove.size();
- if (removeCount == this.selection.length) {
- this.selection = null;
- } else {
- int validCount = 0;
- TreePath[] newSelection = new TreePath[this.selection.length - removeCount];
-
- for(int var10 = 0; var10 < oldCount; ++var10) {
- if (this.selection[var10] != null) {
- newSelection[validCount++] = this.selection[var10];
- }
- }
-
- this.selection = newSelection;
- }
-
- if (this.leadPath == null && this.selection != null) {
- this.leadPath = this.selection[0];
- }
-
- this.updateLeadIndex();
- this.resetRowSelection();
- this.notifyPathChange(pathsToRemove, beginLeadPath);
- }
- }
- }
-
- }
-
- public void removeTreeSelectionListener(TreeSelectionListener x) {
- EventListenerList var10000 = this.listenerList;
- Class var10001 = class$com$sun$java$swing$event$TreeSelectionListener;
- if (var10001 == null) {
- try {
- var10001 = Class.forName("com.sun.java.swing.event.TreeSelectionListener");
- } catch (ClassNotFoundException var2) {
- throw new NoClassDefFoundError(((Throwable)var2).getMessage());
- }
-
- class$com$sun$java$swing$event$TreeSelectionListener = var10001;
- }
-
- var10000.remove(var10001, x);
- }
-
- public void resetRowSelection() {
- this.listSelectionModel.clearSelection();
- if (this.selection != null && this.rowMapper != null) {
- int[] rows = this.rowMapper.getRowsForPaths(this.selection);
- int counter = 0;
-
- for(int maxCounter = this.selection.length; counter < maxCounter; ++counter) {
- int aRow = rows[counter];
- if (aRow != -1) {
- this.listSelectionModel.addSelectionInterval(aRow, aRow);
- }
- }
-
- if (this.leadIndex != -1) {
- this.leadRow = rows[this.leadIndex];
- }
-
- this.insureRowContinuity();
- } else {
- this.leadRow = -1;
- }
-
- }
-
- public void setRowMapper(RowMapper newMapper) {
- this.rowMapper = newMapper;
- this.resetRowSelection();
- }
-
- public void setSelectionMode(int mode) {
- int oldMode = this.selectionMode;
- this.selectionMode = mode;
- if (this.selectionMode != 1 && this.selectionMode != 2 && this.selectionMode != 4) {
- this.selectionMode = 4;
- }
-
- if (oldMode != this.selectionMode && this.changeSupport != null) {
- this.changeSupport.firePropertyChange("selectionMode", new Integer(oldMode), new Integer(this.selectionMode));
- }
-
- }
-
- public void setSelectionPath(TreePath path) {
- if (path == null) {
- this.setSelectionPaths((TreePath[])null);
- } else {
- TreePath[] newPaths = new TreePath[1];
- newPaths[0] = path;
- this.setSelectionPaths(newPaths);
- }
-
- }
-
- public void setSelectionPaths(TreePath[] pPaths) {
- TreePath[] paths = pPaths;
- int newCount;
- if (pPaths == null) {
- newCount = 0;
- } else {
- newCount = pPaths.length;
- }
-
- int oldCount;
- if (this.selection == null) {
- oldCount = 0;
- } else {
- oldCount = this.selection.length;
- }
-
- if (newCount + oldCount != 0) {
- if (this.selectionMode == 1) {
- if (newCount > 1) {
- paths = new TreePath[]{pPaths[0]};
- newCount = 1;
- }
- } else if (this.selectionMode == 2 && newCount > 0 && !this.arePathsContiguous(pPaths)) {
- paths = new TreePath[]{pPaths[0]};
- newCount = 1;
- }
-
- int validCount = 0;
- TreePath beginLeadPath = this.leadPath;
- Vector cPaths = new Vector(newCount + oldCount);
- this.leadPath = null;
-
- for(int newCounter = 0; newCounter < newCount; ++newCounter) {
- boolean found = false;
- if (paths[newCounter] != null) {
- ++validCount;
-
- for(int oldCounter = 0; oldCounter < oldCount; ++oldCounter) {
- if (this.selection[oldCounter] != null && this.selection[oldCounter].equals(paths[newCounter])) {
- this.selection[oldCounter] = null;
- oldCounter = oldCount;
- found = true;
- }
- }
-
- if (!found) {
- cPaths.addElement(new PathPlaceHolder(paths[newCounter], true));
- }
-
- if (this.leadPath == null) {
- this.leadPath = paths[newCounter];
- }
- }
- }
-
- for(int var12 = 0; var12 < oldCount; ++var12) {
- if (this.selection[var12] != null) {
- cPaths.addElement(new PathPlaceHolder(this.selection[var12], false));
- }
- }
-
- if (validCount == 0) {
- this.selection = null;
- } else if (validCount != newCount) {
- this.selection = new TreePath[validCount];
- int var11 = 0;
-
- for(int var13 = 0; var11 < newCount; ++var11) {
- if (paths[var11] != null) {
- this.selection[var13++] = paths[var11];
- }
- }
- } else {
- this.selection = new TreePath[paths.length];
- System.arraycopy(paths, 0, this.selection, 0, paths.length);
- }
-
- if (this.selection != null) {
- this.insureUniqueness();
- }
-
- this.updateLeadIndex();
- this.resetRowSelection();
- if (cPaths.size() > 0) {
- this.notifyPathChange(cPaths, beginLeadPath);
- }
- }
-
- }
-
- public String toString() {
- int selCount = this.getSelectionCount();
- StringBuffer retBuffer = new StringBuffer();
- int[] rows;
- if (this.rowMapper != null) {
- rows = this.rowMapper.getRowsForPaths(this.selection);
- } else {
- rows = null;
- }
-
- retBuffer.append(this.getClass().getName() + " " + this.hashCode() + " [ ");
-
- for(int counter = 0; counter < selCount; ++counter) {
- if (rows != null) {
- retBuffer.append(this.selection[counter].toString() + "@" + Integer.toString(rows[counter]) + " ");
- } else {
- retBuffer.append(this.selection[counter].toString() + " ");
- }
- }
-
- retBuffer.append("]");
- return retBuffer.toString();
- }
-
- protected void updateLeadIndex() {
- if (this.leadPath != null) {
- if (this.selection == null) {
- this.leadPath = null;
- this.leadIndex = this.leadRow = -1;
- } else {
- this.leadRow = this.leadIndex = -1;
-
- for(int counter = this.selection.length - 1; counter >= 0; --counter) {
- if (this.selection[counter].equals(this.leadPath)) {
- this.leadIndex = counter;
- break;
- }
- }
- }
- }
-
- }
-
- private void writeObject(ObjectOutputStream s) throws IOException {
- s.defaultWriteObject();
- Object[] tValues;
- if (this.rowMapper != null && this.rowMapper instanceof Serializable) {
- tValues = new Object[]{"rowMapper", this.rowMapper};
- } else {
- tValues = new Object[0];
- }
-
- s.writeObject(tValues);
- }
- }
-